package com.ice_col.surfaceviewturntable;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.SurfaceHolder;

import java.util.Random;

/**
 * Created by Administrator on 2015/9/11 0011.
 */
public class Turntable extends SurfaceViewTemp{


    private static final String[] mRewardName={"一坨翔","人品1000","贱人王","人品1000","逗比海","人品1000"};

    private static final int[] mRewardIcon={
            R.drawable.ytx,R.drawable.thanks,
            R.drawable.wxl,R.drawable.thanks ,
            R.drawable.hiage,R.drawable.thanks };

    private static final int ITEM_NUM=6;


    //用于将image转成bitmap
    private Bitmap[] mRewardBitmaps;
    //区域颜色
    private static final int[] mAreaColor={0xFFFFC300,0xFFF17E01,0xFFFFC300,0xFFF17E01,0xFFFFC300,0xFFF17E01};

    private int mCenter;
    private int mPadding;
    private float mRadius;
    private RectF mTurntable=new RectF();


    //用于绘制转盘
    private Paint mArcPaint;
    //绘制文字
    private Paint mTextPaint;
    private float mTextSize= TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_SP,20,getResources()
                    .getDisplayMetrics());
    //
    private Paint mBgPaint;
    private float mSpeed;
    private volatile float mStartAngle;
    private boolean mShouldEnd;


    public Turntable(Context context) {
        super(context);
    }

    public Turntable(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width=Math.min(getMeasuredWidth(),getMeasuredHeight());
//        System.out.println("width:"+width);
        mCenter=width/2;
        mPadding=getPaddingLeft();//以padiingleft为准
        mRadius=(width-2*mPadding)/2;
        setMeasuredDimension(width, width);
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        initAttribute();
        super.surfaceCreated(holder);
    }

    private void initAttribute(){
        mRewardBitmaps=new Bitmap[ITEM_NUM];
        for(int i=0;i<ITEM_NUM;i++){
            mRewardBitmaps[i]= BitmapFactory.decodeResource(getResources(),mRewardIcon[i]);
        }
        mTurntable=new RectF(mPadding+10,mPadding+10,getMeasuredWidth()-mPadding-10,getMeasuredHeight()-mPadding-10);
        mArcPaint=new Paint();
        mArcPaint.setAntiAlias(true);
        mTextPaint=new Paint();
        mTextPaint.setColor(Color.WHITE);
        mTextPaint.setTextSize(mTextSize);
        mTextPaint.setAntiAlias(true);
        mBgPaint=new Paint();
        mBgPaint.setColor(0xFFFF0000);
        mBgPaint.setAntiAlias(true);
        mSpeed=0;
        mStartAngle=0;
        mShouldEnd=true;
    }

    @Override
    protected void doDraw() {
        drawBg();
        drawArcArea();//绘制盘块
        drawPointer();
        doTurning();
    }

    private void drawBg(){
        mCanvas.drawColor(Color.WHITE);
        mCanvas.drawCircle(mCenter, mCenter, mRadius, mBgPaint);
        System.out.print("Radius:" + mRadius);
    }

    private void drawArcArea(){
        float arcAngle=360/ITEM_NUM,startAngle=mStartAngle;
        for(int i=0;i<ITEM_NUM;i++,startAngle+=arcAngle){
            mArcPaint.setColor(mAreaColor[i]);
            mCanvas.drawArc(mTurntable, startAngle, arcAngle, true, mArcPaint);
            drawText(startAngle,arcAngle,i);
            drawIcon(i,startAngle);
        }

    }

    private void drawText(float startAngle,float arcAngle,int i){
        Path path=new Path();
        path.addArc(mTurntable, startAngle, arcAngle);
        float textLength=mTextPaint.measureText(mRewardName[i]);
        float arcLength=(float) Math.PI*mRadius*2/ITEM_NUM;

        float hOffset=(arcLength-textLength)/2;
        float vOffset=mRadius/8;
        mCanvas.drawTextOnPath(mRewardName[i],path,(int)hOffset,(int)vOffset,mTextPaint);
    }


    private void drawIcon(int i,float tmpAngle){
        float iconWidth=mRadius/4;
        double angle=(double)(tmpAngle+360/ITEM_NUM/2)*Math.PI/180;
        float centerX=(float) (mCenter+Math.cos(angle)*mRadius/2);
        float centerY=(float) (mCenter+Math.sin(angle)*mRadius/2);
        RectF rec=new RectF(centerX-iconWidth/2,centerY-iconWidth/2,centerX+iconWidth/ 2, centerY + iconWidth / 2);
        mCanvas.drawBitmap(mRewardBitmaps[i], null, rec, null);
    }

    private void drawPointer(){
        mCanvas.drawCircle(mCenter,mCenter,mRadius/6,mBgPaint);
        Path path = new Path();
        path.moveTo(mCenter, mRadius*4/5);// 此点为多边形的起点
        path.lineTo(mCenter+mRadius/12, mCenter);
        path.lineTo(mCenter - mRadius / 12, mCenter);
        path.close(); // 使这些点构成封闭的多边形
        mCanvas.drawPath(path, mBgPaint);
    }


    public void doTurning(){
        mStartAngle=mStartAngle+mSpeed;
        if(mShouldEnd){
            if(mSpeed<=0){
                mSpeed=0;
            }else{
                mSpeed--;
            }
        }
    }


    private float caculateSpeed(int index){
        float arcAngle=360/ITEM_NUM;
        float startAngle=270-(index)*arcAngle;
        float endAngle=startAngle+arcAngle;

        float indexFrom=360*5+startAngle;
        float indexEnd=360*5+endAngle;

        float speedV1=(float) ((-1+Math.sqrt(1+8*indexFrom))/2);
        float speedV2=(float) ((-1+Math.sqrt(1+8*indexEnd))/2);

        float speed=(float)  (speedV1+(speedV2-speedV1)*Math.random());
        return speed;
    }

    public boolean isTurning(){
        return mSpeed!=0;
    }

    public void setShouldEnd(boolean shouldEnd){
        this.mShouldEnd=shouldEnd;
    }

    public void setmSpeed(int index){
        this.mSpeed=caculateSpeed(index);
    }
    public void setmStartAngle(float startAngle){
        this.mStartAngle=startAngle;
    }

    public boolean ismShouldEnd(){
        return mShouldEnd;
    }

}
